﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using CollectionFactories;
using PivotServerTools;

namespace PeopleSearchPivot.WebParts
{
   
    public class SearchPivotViewerWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/PeopleSearchPivot/SearchPivotViewer/SearchPivotViewerWebPartUserControl.ascx";

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Pivot Properties")]               
        [WebDisplayName("Collection name")]
        [Description("Type a cxml collection name.")]        
        public string Collection { get; set; }


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)] 
        [Category("Pivot Properties")]        
        [WebDisplayName("Search query")]
        [Description("Type a search query here.")]        
        public string Query {
            get {
                return SPPivotSettings.Query;                
            }
            set {               
                    SPPivotSettings.Query = value;                                  
            }
        }


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Pivot Properties")]
        [WebDisplayName("Default image")]
        [Description("Path to default image.")]
        public string DefaultImage
        {
            get
            {
                return SPPivotSettings.DefaultImage;
            }
            set
            {
                SPPivotSettings.DefaultImage = value;
            }
        }


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Pivot Properties")]
        [WebDisplayName("Xslt")]
        [Description("Xslt transformation for query result.")]
        public string Xslt
        {
            get
            {
                return SPPivotSettings.Xslt;
            }
            set
            {
                SPPivotSettings.Xslt = value;
            }
        }


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Pivot Properties")]
        [WebDisplayName("FacetCategories")]
        [Description("FacetCategories settings xml.")]
        public string FacetCategories
        {
            get
            {
                return SPPivotSettings.FacetCategories;
            }
            set
            {
                SPPivotSettings.FacetCategories = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Pivot Properties")]
        [WebDisplayName("Maximum query result")]
        [Description("Maximum search query result.")]
        public int MaxItems
        {
            get
            {
                return SPPivotSettings.MaxItems == null ? 0 : SPPivotSettings.MaxItems.Value;
            }
            set
            {
                SPPivotSettings.MaxItems = value;
            }
        }



        protected override void CreateChildControls()
        {
            CollectionFactoryBase collection;
            if (this.Collection.Equals(SharedPicturesFactory.CollectionName))
            {
                collection = new SharedPicturesFactory() as CollectionFactoryBase;

            }
            else
            {
                collection = new SPPeopleSearch() as CollectionFactoryBase;                
            }



            collection.EnsureSettings();

            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }
    }
}
